package com.tinyengine.it.controller.endpoint;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.entity.ApiEnvironment;
import com.tinyengine.it.model.vo.ApiEnvironmentVo;
import com.tinyengine.it.service.endpoint.ApiEnvironmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/env/api")
@Tag(name = "接口环境")
public class ApiEndPointEnvController {

    private final ApiEnvironmentService apiEnvironmentService;

    @Operation(summary = "获取环境集合",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = ApiEnvironment.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    //@SystemControllerLog(description = "获取项目、服务、分组TREE")
    @GetMapping("/getEnvList")
    public Result<List<ApiEnvironment>> getEnvList(@RequestHeader(required = false) Integer appId) {
        LambdaQueryWrapper<ApiEnvironment> lambdaQuery = Wrappers.lambdaQuery();
        if (ObjUtil.isNotNull(appId)) {
            lambdaQuery.eq(ApiEnvironment::getAppId, appId);
        }
        List<ApiEnvironment> projects = apiEnvironmentService.list(lambdaQuery);
        return Result.success(projects);
    }


    @Operation(summary = "新增及更新",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = ApiEnvironment.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    //@SystemControllerLog(description = "获取项目、服务、分组TREE")
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(@RequestBody ApiEnvironmentVo apiEnvironmentVo, @RequestHeader(required = false) Integer appId) {
        ApiEnvironment apiEnvironment = BeanUtil.copyProperties(apiEnvironmentVo, ApiEnvironment.class);
        apiEnvironment.setAppId(appId);
        apiEnvironmentService.saveOrUpdate(apiEnvironment);
        return Result.success(apiEnvironment.getId());
    }


    @Operation(summary = "根据ID删除",
            parameters = {
                    @Parameter(name = "id", description = "ID")},
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    //@SystemControllerLog(description = "获取项目、服务、分组TREE")
    @GetMapping("/deleteById/{id}")
    public Result deleteEnv(@Valid @PathVariable Integer id) {
        apiEnvironmentService.removeById(id);
        return Result.success();
    }
}
